package cn.commonblog.common.aspect;


import cn.commonblog.common.annotation.RequestLog;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;

@Slf4j
@Aspect
@Component
public class RequestLogAspect {

    /**
     * 换行符
     */
    private static final String LINE_SEPARATOR = System.lineSeparator();


    /**
     * 以自定义 @RequestLog 注解为切点
     */
    @Pointcut("@annotation(cn.commonblog.common.annotation.RequestLog)")
    public void requestLog() {

    }

    /**
     * 环绕
     *
     * @param proceedingJoinPoint return
     * @throws Throwable
     */
    @Around("requestLog()")
    public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        long startTime = System.currentTimeMillis();
        //执行切点
        Object result = proceedingJoinPoint.proceed();

        //打印出参
        log.info("Response Args  : {}", new Gson().toJson(result));
        //执行耗时
        log.info("Time-Consuming : {} ms", System.currentTimeMillis() - startTime);

        return result;
    }

    /**
     * 在切点之前织入
     *
     * @param joinPoint
     * @throws Throwable
     */
    @Before("requestLog()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
        //开始打印请求日志
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        //获取 @RequestLog 注解的描述信息
        String methodDescription = getAspectLogDescription(joinPoint);

        //打印请求相关参数
        log.info("================================================== Start ==================================================");
        //打印请求url
        log.info("URL            ：{}", request.getRequestURL().toString());
        //打印描述信息
        log.info("Description    ：{}", methodDescription);
        //打印 Http method
        log.info("HTTP Method    ：{}", request.getMethod());
        //打印调用controller 的全路径以及执行方法
        log.info("Class Method   ：{}.{}", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName());
        //打印请求的IP
        log.info("IP             ：{}", request.getRemoteAddr());
        //打印请求入参
        log.info("Request Args   ：{}", new Gson().toJson(joinPoint.getArgs()));
    }

    /**
     * 在切点之后织入
     */
    @After("requestLog()")
    public void doAfter() {
        //接口结束后换行, 方便分割查看
        log.info("================================================== end ==================================================" + LINE_SEPARATOR);
    }

    public String getAspectLogDescription(JoinPoint joinPoint) throws Exception {

        String targetName = joinPoint.getTarget().getClass().getName();

        String methodName = joinPoint.getSignature().getName();

        Object[] arguments = joinPoint.getArgs();

        Class targetClass = Class.forName(targetName);

        Method[] methods = targetClass.getMethods();

        StringBuilder description = new StringBuilder("");

        for (Method method : methods) {

            if (method.getName().equals(methodName)) {

                Class[] clazzs = method.getParameterTypes();

                if (clazzs.length == arguments.length) {

                    description.append(method.getAnnotation(RequestLog.class).description());

                    break;
                }
            }
        }

        return description.toString();
    }

}
